package com.example.firstassignment;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.graphics.*;

public class ColorDisplay extends Activity {

	
	private Button	displayColorButton;
	private AlertDialog alert;
	private EditText redInput, greenInput, blueInput;
	private TextView displayColorCode;
	private FrameLayout displayColor;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.colordisplay);
		
		
		displayColorButton = (Button) findViewById(R.id.buttonDisplayColor);
		displayColorButton.setOnClickListener(new buttonDisplayColorClick());
		
		redInput = (EditText)findViewById(R.id.redInput);
		greenInput = (EditText)findViewById(R.id.greenInput);
		blueInput = (EditText)findViewById(R.id.blueInput);
		
		displayColorCode = (TextView)findViewById(R.id.displaycolorcode);
		
		displayColor = (FrameLayout)findViewById(R.id.colordisplay);
		
		alert = setup_alert();
	}
	
	
	private class buttonDisplayColorClick implements View.OnClickListener {
		public void onClick(View v){
			int red = 0, green = 0, blue = 0;

			try{
				red = Integer.parseInt(redInput.getText().toString().trim());
				green = Integer.parseInt(greenInput.getText().toString().trim());
				blue = Integer.parseInt(blueInput.getText().toString().trim());
			}
			catch(NumberFormatException nfe){
				alert.show();
			}
			
			if(checkCorrectInput(red, green, blue)){
				int color = mixColor(red, green, blue);
	
				displayColorCode.setText("Color(Hex):#"+Integer.toHexString(color));
				displayColor.setBackgroundColor(color);
			
			}
			else{
				alert.show();
			}
		}
	}
	
	private boolean checkCorrectInput(int red, int green, int blue){
		if( ( (255 >= red) && (red >= 0 ) ) && 
				( (255 >= green) && (green >= 0 ) ) && 
				( (255 >= blue) && (blue >= 0 ) ) ){

			return true;
		}
		return false;
	}
	
	private int mixColor( int red, int green, int blue ){
		int alpha = 255;
		
		return Color.argb(alpha, red,green,blue);
	}
	
    /* AlertDialog Setup and Handling */
    private AlertDialog setup_alert() {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(getResources().getString(R.string.correctNumber));
        builder.setCancelable(false);
        builder.setTitle(getResources().getString(R.string.enterNumber)+" "+getResources().getString(R.string.betweenColorCode));
        builder.setPositiveButton(getResources().getString(R.string.done), new DialogDone());
        return builder.create();
    }
    
    private class DialogDone implements DialogInterface.OnClickListener {
    	public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    }
	
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}

}
